//
//  AppOpenView.m
//  suRead
//
//  Created by 师爱华 on 17/9/21.
//  Copyright © 2017年 bao. All rights reserved.
//

#import "AppOpenView.h"
#import "UIImageView+WebCache.h"
#import "UIControl+Blocks.h"
#import "M_AppOpenImage.h"
#import "NewsWebViewController.h"
#import "Masonry.h"
#import <SDWebImage.h>

@interface AppOpenView()

@property(nonatomic,assign)int countDown; // 倒数计时用
@property(nonatomic,strong) UILabel *timeLabel;
@property (nonatomic,strong) UIButton *button;
@property(nonatomic,strong)NSTimer *timer; // timer

@end

@implementation AppOpenView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        
        self.adImageview = [UIImageView new];
        [self addSubview:self.adImageview];
        [self.adImageview mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.adImageview.superview);
        }];
        if(!self.waitSec) {
            self.waitSec = 3;
        }
                
        __weak typeof(self) weak_self = self;
        UIImage *openImage = [[SDImageCache  sharedImageCache] imageFromDiskCacheForKey:@"kctappopenimg_real"];
        
        if (openImage) {
            self.adImageview.image = openImage;
        }
        
        self.timeLabel = [UILabel new];
        self.timeLabel.layer.cornerRadius = 3.0f;
        self.timeLabel.layer.masksToBounds = YES;
        self.timeLabel.backgroundColor = [[UIColor alloc] initWithRed:0 green:0 blue:0 alpha:0.7];
        self.timeLabel.textColor = [UIColor whiteColor];
        [self addSubview:self.timeLabel];
        [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.timeLabel.superview).offset(-15.0f);
            make.top.equalTo(self.timeLabel.superview).offset(44.0f);
        }];
        UITapGestureRecognizer *jumpTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(jumpTo:)];
        self.timeLabel.userInteractionEnabled = YES;
        [self.timeLabel addGestureRecognizer:jumpTap];
        
        _countDown = self.waitSec;
        NSString *countText = [NSString stringWithFormat:@"  %d   跳过  ",_countDown];
        [self.timeLabel setText:countText];
        
        [self startCountDown];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(self.waitSec * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            UIApplication *application=[UIApplication sharedApplication];
            [application setStatusBarHidden:NO];
            [self removeFromSuperview];
        });
    }
    return self;
}

- (void)jumpTo:(id)sender {
    [self removeFromSuperview];
}

-(void)viewDidDisappear:(BOOL)animated {
    [self viewDidDisappear:animated];
    [self stopTimer];
}

/**
 *  停止倒计时
 *  别小看销毁定时器，没用好可就内存泄漏咯
 */
- (void)stopTimer {
    if (_timer) {
        [_timer invalidate];
    }
}

/**
 *  开始倒计时
 */
-(void)startCountDown {
    _countDown = self.waitSec; //< 重置计时
    _timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES];
    //< 需要加入手动RunLoop，需要注意的是在NSTimer工作期间self是被强引用的
    [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes]; //< 使用NSRunLoopCommonModes才能保证RunLoop切换模式时，NSTimer能正常工作。
}

/**
 *  倒计时逻辑
 */
-(void)timerFired:(NSTimer *)timer {
    
    switch (_countDown) {
        case 1:
            NSLog(@"重新发送");
            
            [self stopTimer];
            break;
        default:
            _countDown -=1;
            NSString *countText = [NSString stringWithFormat:@"  %d   跳过  ",_countDown];
            [self.timeLabel setText:countText];
            break;
    }
}

@end
